﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for TabletimeHandler
    /// </summary>
    public class eng_TabletimeHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
             #region Table Time     
            string add_tabletime = context.Request["add_tabletime"];
            if (!string.IsNullOrEmpty(add_tabletime))
            {
               BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
               var obj = bussiness.InsertOrUpdate<eng_tabletime>(add_tabletime);
               context.Response.Write("Success Full");            
            }
            string view_tabletime = context.Request["view_tabletime"];
            if (!string.IsNullOrEmpty(view_tabletime))
            {
                string Id = context.Request["Id"];
                RenderSingleTableTime(context, Convert.ToInt32(Id));
            }
            string list_tabletime = context.Request["list_tabletime"];
            if (!string.IsNullOrEmpty(list_tabletime))
            {
                RenderTableTime(context);
            }
            string delete_tabletime = context.Request["delete_tabletime"];
            if (!string.IsNullOrEmpty(delete_tabletime))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_tabletime>(delete_tabletime);
                RenderTableTime(context);
            }
            string lockunlock_tabletime = context.Request["lockunlock_tabletime"];
            if (!string.IsNullOrEmpty(lockunlock_tabletime))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_tabletime>(lockunlock_tabletime, "tabletime_active");
                RenderTableTime(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void RenderSingleTableTime(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_tabletime>(
               c => c.tabletime_id.Equals(Id),
               c => new
               {
                   id = c.tabletime_id,
                   year = c.year,
                   month=c.month,
                   week = c.week,
                   monday=c.monday,
                   tuesday=c.tuesday,
                   wednesday=c.wednesday,
                   thursday = c.thursday,
                   triday = c.triday,
                   saturday = c.saturday,
                   sunday = c.sunday,
                   active = c.tabletime_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderTableTime(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_tabletime>(
                null,
                null,
                null,
                null,
                c => new
                {
                    id = c.tabletime_id,
                    year = c.year,
                    month = c.month,
                    week = c.week,
                    monday = c.monday,
                    tuesday = c.tuesday,
                    wednesday = c.wednesday,
                    thursday = c.thursday,
                    triday = c.triday,
                    saturday = c.saturday,
                    sunday = c.sunday,
                    active = c.tabletime_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}